package cn.calm.swing.osgi.file.New;

import java.util.Hashtable;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import cn.calm.swing.osgi.api.MenuService;
import cn.calm.swing.osgi.file.New.tracker.WizardItemServiceTracker;
import cn.calm.swing.osgi.file.New.ui.NewMenuService;
import cn.calm.swing.osgi.file.New.ui.WizardDialog;

public class Activator implements BundleActivator {
	private ServiceRegistration<MenuService> register;
	private ServiceRegistration<WizardDialog> wizardDialogRegister ;
	private WizardItemServiceTracker wizardItemServiceTracker;
	@Override
	public void start(BundleContext context) throws Exception {
		Hashtable<String, String> para = new Hashtable<String, String>();
		para.put(MenuService.MENU_TREE, "file/new");
		MenuService service = new NewMenuService(context);
		register = context.registerService(MenuService.class, service, para);
		WizardDialog dialog=new WizardDialog(context);
		wizardItemServiceTracker=new WizardItemServiceTracker(context, dialog.getRoot(), dialog.getTree());
		wizardItemServiceTracker.open();
		wizardDialogRegister = context.registerService(WizardDialog.class, dialog, null);
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		if (register != null) {
			register.unregister();
			register = null;
		}
		if (wizardDialogRegister != null) {
			wizardDialogRegister.unregister();
			wizardDialogRegister = null;
		}
		if(wizardItemServiceTracker!=null){
			wizardItemServiceTracker.close();
		}
	}
}
